package ynu.sxp.demoapp.product.service;

import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.sxp.demoapp.product.dto.ProductRO;
import ynu.sxp.demoapp.product.dto.TagRO;
import ynu.sxp.demoapp.product.entity.ProductEntity;
import ynu.sxp.demoapp.product.entity.TagEntity;
import ynu.sxp.demoapp.product.repository.ProductRepository;
import ynu.sxp.demoapp.product.repository.TagRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ProductService {
    private final TagService tagService;
    private final ProductRepository productRepository;
    private final TagRepository tagRepository;

    @Autowired
    public ProductService(ProductRepository productRepository, TagService tagService, TagRepository tagRepository) {
        this.productRepository = productRepository;
        this.tagService = tagService;
        this.tagRepository = tagRepository;
    }

    @Transactional
    public ProductEntity addProduct(ProductRO productRO) {
        List<TagEntity> tagEntityList = new ArrayList<>();
        for (TagRO tagRO : productRO.getTags()) {
            TagEntity tagEntity = tagRepository.findByName(tagRO.getName());
            if (tagEntity == null) {
                tagEntity = tagService.addTag(tagRO);
                tagRepository.save(tagEntity);
            }
            tagEntityList.add(tagEntity);
        }
        ProductEntity productEntity = new ProductEntity();
        productEntity.setTitle(productRO.getTitle());
        productEntity.setDescription(productRO.getDescription());
        productEntity.setType(productRO.getType());
        productEntity.setPrice(productRO.getPrice());
        productEntity.setImage(productRO.getImage());
        productEntity.setTags(tagEntityList);
        return productRepository.save(productEntity);
    }

    @Transactional
    public ProductEntity updateProduct(ProductEntity productEntity) {
        return productRepository.save(productEntity);
    }

    @Transactional
    public void deleteProduct(UUID id) {
        Optional<ProductEntity> optionalProduct = productRepository.findById(id);
        if (optionalProduct.isPresent()) {
            productRepository.delete(optionalProduct.get());
        } else {
            throw new EntityNotFoundException("Product not found with ID: " + id);
        }
    }

    @Transactional
    public List<ProductRO> getAllProducts() {
        List<ProductEntity> productEntities = productRepository.findAll();
        //优化
        if (productEntities == null || productEntities.isEmpty()) {
            throw new EntityNotFoundException("商品列表不存在/为空");
        }

        return productEntities.stream()
                .map(productEntity -> {
                    List<TagRO> tagROs = productEntity.getTags().stream()
                            .map(tagEntity -> new TagRO(tagEntity.getName()))
                            .collect(Collectors.toList());

                    return new ProductRO(
                            productEntity.getId(),
                            productEntity.getTitle(),
                            productEntity.getType(),
                            productEntity.getDescription(),
                            productEntity.getPrice(),
                            productEntity.getImage(),
                            tagROs
                    );
                })
                .collect(Collectors.toList());
    }

    @Transactional
    public ProductRO getProduct(UUID id) {
        Optional<ProductEntity> optionalProduct = productRepository.findById(id);
        if (optionalProduct.isPresent()) {
            ProductEntity productEntity = optionalProduct.get();

            ProductRO productRO = new ProductRO();
            productRO.setId(productEntity.getId());
            productRO.setTitle(productEntity.getTitle());
            productRO.setType(productEntity.getType());
            productRO.setDescription(productEntity.getDescription());
            productRO.setPrice(productEntity.getPrice());
            productRO.setImage(productEntity.getImage());

            List<TagRO> tagROList = new ArrayList<>();
            for (TagEntity tagEntity : productEntity.getTags()) {
                TagRO tagRO = new TagRO();
                tagRO.setName(tagEntity.getName());
                tagROList.add(tagRO);
            }
            productRO.setTags(tagROList);

            return productRO;
        }else{
            throw new EntityNotFoundException("商品不存在");
        }
    }
}
